package 曾世昌12_20.demo3;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        List<Element> elements = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_20\\demo3\\web.xml" ).
                getRootElement().elements( "servlet" );
        List<Element> elemap = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_20\\demo3\\web.xml" ).
                getRootElement().elements( "servlet-mapping" );
        for (Element elemapp : elemap) {
            for (Element element : elements) {
                if (elemapp.elementText( "servlet-name" ).equals( element.elementText( "servlet-name" ) )) {
                    map.put( elemapp.elementText( "url-pattern" ), element.elementText( "servlet-class" ) );
                }
            }
        }
        System.out.println( map );
        System.out.println( "输入一个路径/s1或/s2" );
        String s = new Scanner( System.in ).nextLine();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().equals( s )) {
                Class<?> aClass = Class.forName( (String) entry.getValue() );
                Method doGet = aClass.getDeclaredMethod( "doGet" );
                doGet.setAccessible( true );
                Object o = aClass.getConstructor().newInstance();
                doGet.invoke( o, null );
            }
        }
    }
}
